package com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.entity.SurTaskManage;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.queryParams.QueParams;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.service.SurTaskManageService;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.vo.SurTaskManageVo;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.vo.UpdateVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/surTaskManage")
@Slf4j
public class SurTaskManageController {
    @Resource
    SurTaskManageService surTaskManageService;

    /**
     * 分页列表
     *
     * @param
     * @return
     */
    @GetMapping("/getPage/{xsemId}/{type}")
    public RestResponse getPage(HttpServletRequest request,@PathVariable("xsemId") String xsemId,@PathVariable("type") String type) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = surTaskManageService.getPage(curPage, pageSize,xsemId,type);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * App分页列表   只根据 事件id查询分页
     *
     * @param
     * @return
     */
    @GetMapping("/getAppPage/{xsemId}")
    public RestResponse getAppPage(HttpServletRequest request,@PathVariable("xsemId") String xsemId,@RequestParam("userId") String userId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = surTaskManageService.getAppPage(curPage, pageSize,xsemId,userId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }




    /**
     * 查询所有数据
     *
     * @param
     * @return
     */
    @GetMapping("/getAllData/{xsemId}/{type}")
    public RestResponse getAllData(@PathVariable("xsemId") String xsemId,@PathVariable("type") String type) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<SurTaskManage> list = surTaskManageService.getAllData(xsemId,type);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存
     *
     * @param surTaskManageVo
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody SurTaskManageVo surTaskManageVo) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            surTaskManageService.save(surTaskManageVo);
            jsonObject.put("message", "保存成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "保存失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param surTaskManage
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody SurTaskManage surTaskManage) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            surTaskManageService.update(surTaskManage);
            jsonObject.put("message", "修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(@RequestParam("id") String id) {
        RestResponse response = null;
        try {
            SurTaskManage surTaskManage=surTaskManageService.findById(id);
            response = RestResponse.succeed(surTaskManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            surTaskManageService.delete(ids);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 样本编号
     * @param eventCode
     * @return
     */
    @GetMapping("/getCode")
    public RestResponse getCode(String eventCode){
        RestResponse response = null;

        try{
            Map<String,Object> map=surTaskManageService.getCode(eventCode);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取当前地震事件下的所有任务
     * @param xsemId
     * @return
     */
    @GetMapping("/getTaskListByXsemId")
    public RestResponse getTaskListByXsemId(String xsemId){
        RestResponse response = null;

        try{
            List<SurTaskManage> list=surTaskManageService.getTaskListByXsemId(xsemId);
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 获取当前地震事件下 所有评估子区2的数据
     * @param xsemId
     * @return
     */
    @GetMapping("/getTaskListByXsemIdAndType")
    public RestResponse getTaskListByXsemIdAndType(HttpServletRequest request,String xsemId, String type){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = surTaskManageService.getTaskListByXsemIdAndType(xsemId,type,curPage,pageSize);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 调查任务状态修改
     * @param
     * @return
     */
    @PostMapping("/updateTaskStatu")
    public RestResponse updateTaskStatu(@RequestBody UpdateVO updateVO){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            surTaskManageService.updateTaskStatu(updateVO);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "修改失败";
            }
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询新建/区划任务数据
     * @param xsemId
     * @return
     */
    @GetMapping("/getTaskByTypeAndXsemId")
    public RestResponse getTaskByTypeAndXsemId(String xsemId,String type){
        RestResponse response = null;

        try{
            List<SurTaskManage> list=surTaskManageService.getTaskByTypeAndXsemId(xsemId);
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
